﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace Things.Web.OrderPlan
{
    public class OrderPlanCartUtil
    {
        public static void Ordering(
            HttpContext context,
            Guid saleSetId,
            Guid goodsId,
            double deliveredCount,
            double deliveredCycle,
            double deliveredNumber,
            string cartPageUrl)
        {
            //往购物车中添加商品
            HttpCookie hc = null;
            if (context.Request.Cookies["OrderPlanChart"] == null)
            {
                //如果Cookies中不存在ShoppingCart，则创建
                hc = new HttpCookie("OrderPlanChart");
            }
            else
            {
                //如果Cookies中存在ShoppingCart，则取出
                hc = context.Request.Cookies["OrderPlanChart"];
            }

            //标记在购物车中是否存在本次选择的物品
            bool isExist = false;

            //在购物车的Cookies中查找是否存在这次要选择的物品
            foreach (string item in hc.Values.Keys)
            {
                if (item == saleSetId.ToString("N"))
                {
                    isExist = true;
                    break;
                }
            }
            if (!isExist)
            {
                //如果选择的内容在购物车中没有，则创建一个新的子键
                hc.Values.Add(saleSetId.ToString("N"), string.Format("{0}|{1}|{2}|{3}|{4}|",
                    saleSetId.ToString("N"),
                    goodsId.ToString("N"),
                    deliveredCount,
                    deliveredCycle,
                    deliveredNumber));
            }
            else
            {
                //如果选择的内容在购物车中没，则删除原来的，添加一个新的
                //int num = int.Parse(hc.Values[id].Split(new char[] { '|' })[3]) + 1;
                hc.Values.Remove(saleSetId.ToString("N"));
                hc.Values.Add(saleSetId.ToString("N"), string.Format("{0}|{1}|{2}|{3}|{4}|",
                    saleSetId.ToString("N"),
                    goodsId.ToString("N"),
                    deliveredCount,
                    deliveredCycle,
                    deliveredNumber));
            }
            hc.Expires = DateTime.Now.AddDays(1);
            context.Response.Cookies.Add(hc);
            context.Response.Redirect(cartPageUrl);
        }

        public static DataTable GetOrders(HttpContext context)
        {
            HttpCookie hc = null;
            if (context.Request.Cookies["OrderPlanChart"] == null)
            {
                return null;
            }
            else
            {
                hc = context.Request.Cookies["OrderPlanChart"];
            }

            DataTable table = new DataTable();
            table.Columns.Add("SetId");
            table.Columns.Add("GoodsId");
            table.Columns.Add("deliveredCount");
            table.Columns.Add("deliveredCycle");
            table.Columns.Add("deliveredNumber");
            for(int index =0; index< hc.Values.Count;index++)
            {
                string[] values = hc.Values[index].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                if (values.Length == 5)
                {
                    DataRow row = table.NewRow();
                    for (int indexCell = 0; indexCell < values.Length; indexCell++)
                    {
                        row[indexCell] = values[indexCell];
                    }
                    table.Rows.Add(row);
                }
            }
            return table;
        }

        public static void Remove(HttpContext context, Guid setId, string cartPageUrl)
        {
            //往购物车中添加商品
            HttpCookie hc = null;
            if (context.Request.Cookies["OrderPlanChart"] != null)
            {
                hc = context.Request.Cookies["OrderPlanChart"];
            }

            //在购物车的Cookies中查找是否存在这次要选择的物品
            foreach (string item in hc.Values.Keys)
            {
                if (item == setId.ToString("N"))
                {
                    hc.Values.Remove(setId.ToString("N"));
                    break;
                }
            }
            hc.Expires = DateTime.Now.AddDays(1);
            context.Response.Cookies.Add(hc);
            context.Response.Redirect(cartPageUrl);
        }

        public static void RemoveAll(HttpContext context)
        {
            //往购物车中添加商品
            HttpCookie hc = null;
            if (context.Request.Cookies["OrderPlanChart"] != null)
            {
                hc = context.Request.Cookies["OrderPlanChart"];
            }

            if (hc != null)
            {
                //在购物车的Cookies中查找是否存在这次要选择的物品
                System.Collections.Specialized.NameObjectCollectionBase.KeysCollection keys = hc.Values.Keys;
                int count = keys.Count;
                for (int i = count - 1; i >= 0; i--)
                {
                    hc.Values.Remove(keys[i]);
                }
                //foreach (string item in hc.Values.Keys)
                //{
                //    hc.Values.Remove(item);
                //}
                hc.Expires = DateTime.Now.AddDays(1);
                context.Response.Cookies.Add(hc);
            }
        }
    }
}